package br.com.projeto.hibernatedao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import br.com.projeto.dao.UserDAO;
import br.com.projeto.entities.User;

public class UserHibernateDAO implements UserDAO{
	
	public Session session;
	
	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}
	
	@Override
	public void saveUser(User user) {
		this.session.save(user);		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<User> listAllUsers() {
		return getSession().createCriteria(User.class).list();
	}

	@Override
	public User loginUser(User user) {
		return (User) getSession().createCriteria(User.class)
				.add(Restrictions.eq("name", user.getName()))
				.add(Restrictions.eq("password", user.getPassword()))
				.uniqueResult();
	}

	@Override
	public void updateUser(User user) {
		this.session.update(user);
	}

	@Override
	public void deleteUser(User user) {
		this.session.delete(user);
	}

}
